const mta = require('../../../mta_sdk/mta_analysis.js');
var ctx = null;
const app = getApp();
var school='';
var classs='';
var name='';
var pid='';
var pic='';
var bgpic='';
var shareImg=''
Page({

  data: {
    shareImg: '',
    shared:0,
    ajaxSuccess:false
  },

  onLoad: function (opt) {
    mta.Page.init();
   
    if (opt.shared==1){
      this.setData({
        shared:1,
        shareImg: opt.shareImg || '',
        ajaxSuccess:true
      });
        
    }
    if (opt.name) {
     
      this.makeImg(opt.school, opt.class, opt.name, opt.pic, opt.bgpic)

    }
      school = opt.school;
      classs = opt.class;
      name = opt.name;
      pid = opt.pid;
      pic = opt.pic;
      bgpic = opt.bgpic
  },

  makeImg(school, classs, name, pic, bgpic) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    });
    
    
    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .06;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");

    
    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.select('#headstyle').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {
      
      var w = res[0].width;
      var h = res[0].height;
      var head_w = res[1].width;
      var head_h = res[1].height;

      wx.getImageInfo({
            src: pic,
            success: x => {
              ctx.drawImage(bgpic, 0, 0, w, h);
            ctx.drawImage(x.path, (w - head_w) / 2, h / 4.2, head_w,head_h);

            ctx.setFontSize(ft);
            ctx.fillStyle = '#222222';
            ctx.fillText(school, w / 2.3, h / 1.76);
            ctx.fillText(classs, w / 2.3, h / 1.54);
            ctx.fillText(name, w / 2.3, h / 1.35);
            ctx.draw(false, function () {
              app.buldingImg("mycanvas", callBack => {
                me.setData({
                  shareImg: callBack.data.url,
                  ajaxSuccess: true
                });
                wx.hideLoading();
              });
            });
            
            }
          })
    })

  },
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(this.data.shareImg)
    return {
      title: '学生证制作',
      desc: '学生证制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
      Success:function(){
        
      }
  }
},
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  toPlay(){
    wx.navigateTo({
      url: `/pages/${pid}/${pid}_make/${pid}_make?pid=${pid}`,
    })
  },
  toHome(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})